﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page 
{
    
    Operation operation = new Operation();  //声明网站业务类对象
    string texts;
    string pics="";
    string links;
    protected void Page_Load(object sender, EventArgs e)    
    {
        if (!IsPostBack)  //!IsPostBack避免重复刷新加载页面
        {
            dlJSGY.DataSource = operation.SelectTbInfo("技术供应信息", 5);
            dlJSGY.DataBind();
            dlJSXQ.DataSource = operation.SelectTbInfo("技术需求信息", 5);
            dlJSXQ.DataBind();
          // dlZP.DataSource = operation.SelectTbInfo("招聘信息", 5);
          //  dlZP.DataBind();
          //  dlQZ.DataSource = operation.SelectTbInfo("求职信息", 5);
         //   dlQZ.DataBind();
            dlXW.DataSource = operation.SelectTbInfo("新闻信息",8);
            dlXW.DataBind();       
        }
    }
    public string getPicUrls()
    {
        DataSet ds = operation.SelectInfo("新闻信息", true);
        int count = ds.Tables[0].Rows.Count;
        int i = 0;
        int picc=0;
        string picurl;
        string text;
        string id;
        pics="";
        texts = "";
        links = "";
        count = count > 5 ? 5 : count;
        while (i < count)
        {
            try
            {
                id = ds.Tables[0].Rows[i][0].ToString();
                picurl = "BackGround/im/" + id + ".jpg";
                if (System.IO.File.Exists(Server.MapPath(picurl)))
                {
                    pics += "|" + picurl;
                    links += "|ShowLeaguerInfo.aspx?id=" + id;
                    text = ds.Tables[0].Rows[i][2].ToString();
                    text = text.Length > 10 ? text.Substring(0, 10) : text;
                    text=text.Replace("\"", "\\\"");
                    text=text.Replace("|", " ");
                    texts += "|"+text;
                    picc++;
                }
            }
            catch (Exception ex) { }
            i++;
        }
        
        while (picc < 5)
        {
            pics += "|BackGround/im/pic" + picc + ".jpg";
            texts += "| ";
            links += "|Default.aspx";
            picc++;
        }
        links = links.Substring(1);
        pics = pics.Substring(1);
        texts = texts.Substring(1);
        return pics;
    }
    public string getTexts()
    {
        return texts;
    }
    public string getLinks()
    {
        return links;
    }
    public string getLists()
    {
        DataSet ds = operation.SelectRecommendInfoByType("技术供应信息",500);
        int count = ds.Tables[0].Rows.Count;
        int i = 0;
        
        string[] lsts;
        string lst="";
        string pic="";
        string url = "ShowLeaguerInfo.aspx?id=";
        string title;
        int n = 0;
        
        for (i = 0; i < count; i++)//获得带图片供应推荐信息
        {
            lsts = GetHtmlImageUrlList(ds.Tables[0].Rows[i][3].ToString());
            //图片地址1#链接地址1#标题1|
            if (lsts.Length > 0)
            {
                title = ds.Tables[0].Rows[i][2].ToString();
                title = title.Replace('\"', ' ');
                title = title.Replace('#', ' ');
                title = title.Replace('|', ' ');
                pic = lsts[0];//.Substring(lsts[0].LastIndexOf("/") + 1);
                lst += pic + "#" + url + ds.Tables[0].Rows[i][0].ToString() + "#"+title + "|";
                n++;
                if (n == 12) break;
            }
        }
        if (n < 6)//不够6条就显示新闻图片
        {
            if (pics == "")
                pics = getPicUrls();
            string[] ps = pics.Split('|');
            string[] ts = texts.Split('|');
            string[] ls = links.Split('|');
            for (i = ps.Length-1; i >= 0; i--)
            {
                lst += ps[i] + "#" + ls[i] + "#" + ts[i] + "|";
                n++;
                if (n ==6) break;
            }
        }
        lst = lst.Substring(0, lst.Length - 1);
        return lst;
    }
    /// <summary>
    /// 取得HTML中所有图片的 URL。//http://club.itqun.net/showtopic-50625.html
/// </summary>
/// <param name="sHtmlText">HTML代码</param>
/// <returns>图片的URL列表</returns>
    public static string[] GetHtmlImageUrlList(string sHtmlText)
    {
        // 定义正则表达式用来匹配 img 标签
        Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);

        // 搜索匹配的字符串
        MatchCollection matches = regImg.Matches(sHtmlText);

        int i = 0;
        string[] sUrlList = new string[matches.Count];

        // 取得匹配项列表
        foreach (Match match in matches)
            sUrlList[i++] = match.Groups["imgUrl"].Value;

        return sUrlList;
    }
}
